#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import init
import initCashFlowPage
from data_alchemy.account_book_alchemy import AccountBookAlchemy
from data_alchemy.account_voucher_alchemy import AccountVoucherAlchemy
from data_alchemy.balance_sheet_alchemy import BalanceSheetAlchemy
import initBalanceSheetPage
from data_alchemy.balance_sheet_monthly_alchemy import BalanceSheetMonthlyAlchemy
from data_alchemy.cash_flow_alchemy import CashFlowAlchemy
from data_alchemy.cash_flow_monthly_alchemy import CashFlowMonthlyAlchemy
from initLogger import logger


class CashFlow:
    '''
    类功能：用于处理现金流表数据的计算工作
    '''

    def __init__(self, year=2022, month=4):
        '''
        函数功能：
        '''

        self.year = year
        self.month = month
        # self.accountBookDetail = {}     #暂时未使用
        # self.propertyIncrease = {}      #当月各科目资产增加情况
        # self.propertyDecrease = {}      #当月各科目资产减少情况



    def isCashFlowCoucherItem(self, voucherItem):
        '''
        函数功能：判断一个记账凭证是否属于现金流原始数据
        :param voucherItem:
        :return:
               True 表示有现金流存在
               False 表示不存在现金流
        '''

        if len(voucherItem.ownership_decrease_name)!=0 and len(voucherItem.ownership_increase_name)!=0:
            #该记账凭证为负债，不涉及现金流。
            return False

        return True

    def genCashFlowInforItem(self, year, month, subjectAttribute, subjectPath):
        '''
        函数功能：获取特定的账簿项信息,并生成资产负债表数据
        :param year:
        :param month:
        :param subjectAttribute:
        :param subjectPath:
        :return:
        cashFlow.addCashFlowInfor(2024, 7, "ownership_decrease_name", "权属->自有->本年结余->日常->支出")
        '''


        accountVoucherAlchemy = AccountVoucherAlchemy()
        investmentVoucherInfor = accountVoucherAlchemy.SearchVoucherByMonthSubject(year, month, subjectAttribute, subjectPath)

        #print(investmentVoucherInfor)

        inflowTotalValue = 0.0
        outflowTotalValue = 0.0


        for investmentVoucherItem in investmentVoucherInfor:
            flag = self.isCashFlowCoucherItem(investmentVoucherItem)
            if(flag==False):
                #print(str(investmentVoucherItem))
                #不产生现金流不做处理
                continue
            if(subjectAttribute=='property_increase_name'):
                inflowTotalValue = inflowTotalValue +  investmentVoucherItem.property_increase_value
                outflowTotalValue = outflowTotalValue + 0.0
            elif(subjectAttribute=='property_decrease_name'):
                inflowTotalValue = inflowTotalValue + 0.0
                outflowTotalValue = outflowTotalValue + investmentVoucherItem.property_decrease_value
            elif(subjectAttribute=='ownership_increase_name'):
                inflowTotalValue = inflowTotalValue +  investmentVoucherItem.ownership_increase_value
                outflowTotalValue = outflowTotalValue + 0.0
            elif(subjectAttribute=='ownership_decrease_name'):
                inflowTotalValue = inflowTotalValue + 0.0
                outflowTotalValue = outflowTotalValue + investmentVoucherItem.ownership_decrease_value
            else:
                logger.debug("call getCashFlowInforItem function error")



        return inflowTotalValue, outflowTotalValue;


    def genCashFlow(self):
        '''
        函数功能：生成现金流表数据
        :return:
        '''
        #删除指定月历史
        cashFlowAlchemy = CashFlowAlchemy()
        cashFlowAlchemy.DelCashFlowByMonth(self.year, self.month)

        cashFlowMonthlyAlchemy = CashFlowMonthlyAlchemy()
        cashFlowMonthlyAlchemy.DelCashFlowByMonth(self.year, self.month)

        self.genPropertyCashFlow()
        self.genOwnershipCashFlow()

    def genPropertyCashFlow(self):
        '''
        函数功能：处理资产现金流数据
        :return:
        '''
        logger.debug("call genPropertyCashFlow function ")
        inflowTotalValue = 0.0
        outflowTotalValue = 0.0
        differentTotalValue= 0.0
        for subjectName in initCashFlowPage.CASH_FLOW_PROPERTY_LIST:
            subjectPath = initCashFlowPage.CASH_FLOW_PROPERTY_LIST[subjectName]
            inflowDecreaseValue, outflowDecreaseValue = self.genCashFlowInforItem(self.year, self.month,
                                                                                        "property_decrease_name",
                                                                                  subjectPath)
            inflowIncreaseValue, outflowIncreaseValue = self.genCashFlowInforItem(self.year, self.month,
                                                                                        "property_increase_name",
                                                                                  subjectPath)
            inflowValue = inflowDecreaseValue + inflowIncreaseValue
            outflowValue = outflowDecreaseValue + outflowIncreaseValue
            differentValue = inflowValue - outflowValue

            inflowTotalValue = inflowTotalValue + inflowValue
            outflowTotalValue = outflowTotalValue + outflowValue
            differentTotalValue = differentTotalValue + differentValue

            cashFlowAlchemy= CashFlowAlchemy(   year=self.year,
                                                month=self.month,
                                                dataType=init.TYPE_PROPERTY,
                                                path=subjectPath,
                                                category=subjectName,
                                                inflowValue=inflowValue,
                                                outflowValue=outflowValue,
                                                differenceValue=differentValue)
            cashFlowAlchemy.AddCashFlowItem()

            logger.debug(" %s:\tinflowValue=%.2f; outflowValue=%.2f；differentValue=%.2f；" % (
            subjectPath, inflowValue, outflowValue, differentValue))


        logger.debug(" %s:\tinflowTotalValue=%.2f; outflowTotalValue=%.2f；differentTotalValue=%.2f" % (
            "资产", inflowTotalValue, outflowTotalValue, differentTotalValue))

        cashFlowMonthlyAlchemy=CashFlowMonthlyAlchemy(  year=self.year,
                                                        month=self.month,
                                                        dataType=init.TYPE_PROPERTY,
                                                        inflowValue=inflowTotalValue,
                                                        outflowValue=outflowTotalValue,
                                                        differenceValue=differentTotalValue)
        cashFlowMonthlyAlchemy.AddCashFlowItem()

    def genOwnershipCashFlow(self):
        '''
        函数功能：生成权属现金流表数据
        :return:
        '''
        logger.debug("call genOwnershipCashFlow function ")
        inflowTotalValue = 0.0
        outflowTotalValue = 0.0
        differentTotalValue= 0.0

        for subjectName in initCashFlowPage.CASH_FLOW_OWNERSHIP_LIST:
            subjectPath = initCashFlowPage.CASH_FLOW_OWNERSHIP_LIST[subjectName]
            inflowDecreaseValue, outflowDecreaseValue= self.genCashFlowInforItem(self.year, self.month, "ownership_decrease_name", subjectPath)
            inflowIncreaseValue, outflowIncreaseValue = self.genCashFlowInforItem(self.year, self.month, "ownership_increase_name", subjectPath)
            inflowValue = inflowDecreaseValue + inflowIncreaseValue
            outflowValue = outflowDecreaseValue + outflowIncreaseValue
            differentValue = inflowValue - outflowValue

            inflowTotalValue = inflowTotalValue + inflowValue
            outflowTotalValue = outflowTotalValue + outflowValue
            differentTotalValue = differentTotalValue + differentValue

            logger.debug(" %s:\tinflowValue=%.2f; outflowValue=%.2f；differentValue=%.2f" %(subjectPath, inflowValue, outflowValue, differentValue))

            cashFlowAlchemy= CashFlowAlchemy(   year=self.year,
                                                month=self.month,
                                                dataType=init.TYPE_OWNERSHIP,
                                                path=subjectPath,
                                                category=subjectName,
                                                inflowValue=inflowValue,
                                                outflowValue=outflowValue,
                                                differenceValue=differentValue)
            cashFlowAlchemy.AddCashFlowItem()

        logger.debug(" %s:\tinflowTotalValue=%.2f; outflowTotalValue=%.2f；differentTotalValue=%.2f" % (
            "权属", inflowTotalValue, outflowTotalValue, differentTotalValue))

        cashFlowMonthlyAlchemy=CashFlowMonthlyAlchemy(  year=self.year,
                                                        month=self.month,
                                                        dataType=init.TYPE_OWNERSHIP,
                                                        inflowValue=inflowTotalValue,
                                                        outflowValue=outflowTotalValue,
                                                        differenceValue=differentTotalValue)
        cashFlowMonthlyAlchemy.AddCashFlowItem()





    def getCashFlowDataMonthly(self, dataType):
        '''
        函数功能：获取指定月份现金流的数据
        :param year:
        :param month:
        :param dataType: 标记资产或是权属
        :return:
        '''

        cashFlowAlchemy = CashFlowAlchemy()
        cashFlowResult = cashFlowAlchemy.SearchCashFlowByMonthDataType(self.year, self.month, dataType)

        return cashFlowResult

    def getCashFlowTotalDataMonthly(self, dataType):
        '''
        函数功能：获取指定月份现金流合计数据
        :param dataType:
        :return:
        '''
        cashFlowMonthlyAlchemy = CashFlowMonthlyAlchemy()
        cashFlowMonthlyResult  = cashFlowMonthlyAlchemy.SearchCashFlowByMonthDataType(self.year, self.month, dataType)
        return cashFlowMonthlyResult

def main():

    cashFlow = CashFlow(year=2024, month=9)
    # cashFlowreusult = cashFlow.getCashFlowDataMonthly(init.TYPE_PROPERTY)
    # cashFlowreusult = cashFlow.getCashFlowTotalDataMonthly(init.TYPE_PROPERTY)

    cashFlow.genCashFlow()
    # cashFlow.addCashFlowInfor(2024, 7, "ownership_increase_name", "权属->自有->本年结余->日常->收入")
    #cashFlow.addCashFlowInfor(2024, 7, "ownership_decrease_name", "权属->自有->本年结余->日常->支出")
    return

if __name__ == '__main__':
    main()